package com.symc.sims0213xfw.service.impl;

import com.symc.sims0213xfw.bean.Classes;
import com.symc.sims0213xfw.bean.Department;
import com.symc.sims0213xfw.dao.DepartmentDao;
import com.symc.sims0213xfw.dao.impl.DepartmentDaoImpl;
import com.symc.sims0213xfw.service.DepartmentService;
import com.symc.sims0213xfw.service.OperationStatusResult;

import java.util.List;

/**
 * @Author: 凤文  沈阳医学院2019级医学信息工程 0213
 * @CreateTime: 2022/01/26 13:53
 * @Description:
 */
public class DepartmentServiceImpl implements DepartmentService {
    private DepartmentDao departmentDao = new DepartmentDaoImpl();

    @Override
    public List<Department> getOptionalDepartmentList() {
        return departmentDao.queryListVisibleDepartment();
    }

    @Override
    public List<Department> getOptionalDepartmentList(String collegeName) {
        return departmentDao.queryDepartmentByCollegeName(collegeName);
    }

    @Override
    public Long addDepartment(Integer id, String name, Integer cid) {
        if (id > Department.ID_MAX || id < Department.ID_MIN) {
            return OperationStatusResult.UNREASONABLE_DID;
        }

        if (name.length() > Department.NAME_LENGTH) {
            return OperationStatusResult.NAME_LENGTH_OUT_OF_BOUNDS;
        }

        if (cid > Department.CID_MAX || cid < Department.CID_MIN) {
            return OperationStatusResult.UNREASONABLE_DID;
        }

        if (departmentDao.queryVisibleDepartment(cid) != null) {//用户可见
            return OperationStatusResult.EXIST;
        } else if (departmentDao.queryDepartment(cid) != null) {//库中有,但不可见
            if (departmentDao.updateDepartment(id, name, cid) > 0) {
                return OperationStatusResult.SUCCESS;
            } else {
                return OperationStatusResult.DEFEAT;
            }
        } else {//库中没有
            if (departmentDao.addDepartment(id, name, cid) > 0) {
                return OperationStatusResult.SUCCESS;
            } else {
                return OperationStatusResult.DEFEAT;
            }
        }
    }

    @Override
    public Long updateDepartment(Integer id, String name, Integer cid) {
        if (id > Department.ID_MAX || id < Department.ID_MIN) {
            return OperationStatusResult.UNREASONABLE_DID;
        }

        if (name.length() > Department.NAME_LENGTH) {
            return OperationStatusResult.NAME_LENGTH_OUT_OF_BOUNDS;
        }

        if (cid > Department.CID_MAX || cid < Department.CID_MIN) {
            return OperationStatusResult.UNREASONABLE_DID;
        }

        if (departmentDao.queryVisibleDepartment(cid) == null) {//用户不可见
            return OperationStatusResult.NOT_EXIST;
        } else {
            if (departmentDao.updateDepartment(id, name, cid) > 0) {
                return OperationStatusResult.SUCCESS;
            } else {
                return OperationStatusResult.DEFEAT;
            }
        }
    }

    @Override
    public Long deleteDepartment(Integer id) {
        if (departmentDao.queryVisibleDepartment(id) == null) {
            return OperationStatusResult.NOT_EXIST;//ok
        } else {
            if (departmentDao.deleteVisibleDepartment(id) > 0) {
                return OperationStatusResult.SUCCESS;
            } else {
                return OperationStatusResult.DEFEAT;
            }
        }
    }
}
